package com.emm.yixun.mobile.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.BusinessAudit;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * 
 * <p>
 * <b>Title：</b> 申请驳回界面
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:09:15
 */
public class RejectActivity extends SwipeBackActivity {

	private static final String TAG = "RejectActivity";

	private String payDepositId;

	/** 标题 */
	TextView title_main;

	/** 返回按钮 */
	ImageView back_btn;

	/** 确定按钮 */
	Button sure_btn;

	/** 驳回输入框 */
	EditText reject_content;

	/** 类型 1认筹 2认购 3签约 */
	String businessType;

	private String taskId;

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;

	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reject_activity);

		SetSwipeBackLayout(mSwipeBackLayout);

		BaseActivity.activity = this;

		businessType = getIntent().getStringExtra("businessType");
		payDepositId = getIntent().getStringExtra("payDepositId");
		taskId = getIntent().getStringExtra("taskId");

		initView();

		SetRelativiLayoutPadding(R.id.main_head_top_rela);

		initBtn();
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:29:17
	 */
	private void initView() {
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText(getString(R.string.from_bing2_ing));
		back_btn = (ImageView) findViewById(R.id.back_btn);
		sure_btn = (Button) findViewById(R.id.sure_btn);
		reject_content = (EditText) findViewById(R.id.reject_content);
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:29:25
	 */
	private void initBtn() {
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		sure_btn.setVisibility(View.VISIBLE);
		sure_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String str = reject_content.getText().toString().trim();
				EmmApplication.disKey(RejectActivity.this);
				if ("1".equals(businessType)) {
					businessAudit("认筹", "2", str);
				} else if ("2".equals(businessType)) {
					businessAudit("认购", "2", str);
				} else if ("3".equals(businessType)) {
					businessAudit("签约", "2", str);
				} else {
					Log.v(TAG, "businessType-->参数传入错误：1=认筹 2=认购 3=签约 ");
				}
			}
		});
	}

	BusinessAudit businessaudit;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 查看认筹详情接口
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param businessType
	 *            业务类型
	 * @param auditStatus
	 *            审核状态
	 * @param remark
	 *            备注
	 * @author tangxin
	 * @date 2016年12月7日 下午4:29:39
	 */
	private void businessAudit(final String businessType, String auditStatus, String remark) {
		EmmApplication.updateUrl("businessAudit");
		Map<String, String> map = new HashMap<String, String>();
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		map.put("projectCode", EmmApplication.getProjectCode());
		map.put("businessId", payDepositId);
		map.put("businessType", businessType);
		map.put("auditStatus", auditStatus);
		map.put("taskId", taskId);
		map.put("remark", remark);
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("businessAudit-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.hideDialogForLoading();
				Loading.showDialogForLoading(RejectActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}
				businessaudit = JSONObject.parseObject(resData.toString(), BusinessAudit.class);
				if ("1".equals(businessaudit.getResult())) {
					Log.v("获取成功", "");
					EmmApplication.T("退回成功");
					sendBroadcast(new Intent(EmmApplication.FROM_LIST_DETAILS_FI_NI_ACTIVITY));
					sendBroadcast(new Intent(EmmApplication.SUB_SC_RI_DETAILS_FI_NI_ACTIVITY));
					sendBroadcast(new Intent(EmmApplication.SIGNED_FI_NI_ACTIVITY));
					finish();
				} else {
					EmmApplication.T(businessaudit.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + businessaudit.getErrorCode().toString() + "errorMsg:"
							+ businessaudit.getErrorMsg().toString());
				}

			}
		});
	}
}
